package com.omni.monitor.plugin.utils;

import org.springframework.http.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Map;

/**
 * Spring RestTemplate 实现的 HTTP 请求工具类
 */
public class SpringHttpUtils {

    // 注入 RestTemplate（Spring 项目中可通过 @Autowired 注入）
    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * GET 请求
     * @param url 请求地址
     * @param responseType 响应数据类型（如 String.class、User.class）
     * @param params URL 参数（可选）
     * @param headers 请求头（可选）
     * @return 响应结果
     * @param <T> 响应数据泛型
     */
    public static <T> T doGet(String url, Class<T> responseType, Map<String, Object> params, HttpHeaders headers) {
        try {
            // 构建请求实体
            HttpEntity<?> requestEntity = new HttpEntity<>(headers);
            // 发送 GET 请求
            ResponseEntity<T> responseEntity = restTemplate.exchange(
                    url, HttpMethod.GET, requestEntity, responseType, params
            );
            return responseEntity.getBody();
        } catch (RestClientException e) {
            throw new RuntimeException("GET 请求失败：" + url, e);
        }
    }

    /**
     * 简化版 GET 请求（无参数、无自定义请求头）
     */
    public static <T> T doGet(String url, Class<T> responseType) {
        return doGet(url, responseType, null, new HttpHeaders());
    }

    /**
     * POST 请求（JSON 格式请求体）
     * @param url 请求地址
     * @param requestBody 请求体（会自动序列化为 JSON）
     * @param responseType 响应数据类型
     * @param headers 请求头（可选，默认设置 Content-Type: application/json）
     * @return 响应结果
     * @param <T> 响应数据泛型
     * @param <R> 请求体泛型
     */
    public static <T, R> T doPostJson(String url, R requestBody, Class<T> responseType, HttpHeaders headers) {
        try {
            // 默认设置 JSON 请求头
            if (headers == null) {
                headers = new HttpHeaders();
            }
            headers.setContentType(MediaType.APPLICATION_JSON);

            // 构建请求实体
            HttpEntity<R> requestEntity = new HttpEntity<>(requestBody, headers);
            // 发送 POST 请求
            ResponseEntity<T> responseEntity = restTemplate.exchange(
                    url, HttpMethod.POST, requestEntity, responseType
            );
            return responseEntity.getBody();
        } catch (RestClientException e) {
            throw new RuntimeException("POST JSON 请求失败：" + url, e);
        }
    }

    /**
     * 简化版 POST JSON 请求（无自定义请求头）
     */
    public static <T, R> T doPostJson(String url, R requestBody, Class<T> responseType) {
        return doPostJson(url, requestBody, responseType, null);
    }

    /**
     * POST 请求（表单格式请求体）
     * @param url 请求地址
     * @param formParams 表单参数
     * @param responseType 响应数据类型
     * @return 响应结果
     * @param <T> 响应数据泛型
     */
    public static <T> T doPostForm(String url, Map<String, Object> formParams, Class<T> responseType) {
        try {
            // 设置表单请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            // 构建表单请求体
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(formParams, headers);
            // 发送 POST 请求
            ResponseEntity<T> responseEntity = restTemplate.exchange(
                    url, HttpMethod.POST, requestEntity, responseType
            );
            return responseEntity.getBody();
        } catch (RestClientException e) {
            throw new RuntimeException("POST 表单请求失败：" + url, e);
        }
    }

    /**
     * PUT 请求（JSON 格式）
     */
    public static <T, R> T doPutJson(String url, R requestBody, Class<T> responseType) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<R> requestEntity = new HttpEntity<>(requestBody, headers);

            ResponseEntity<T> responseEntity = restTemplate.exchange(
                    url, HttpMethod.PUT, requestEntity, responseType
            );
            return responseEntity.getBody();
        } catch (RestClientException e) {
            throw new RuntimeException("PUT 请求失败：" + url, e);
        }
    }

    /**
     * DELETE 请求
     */
    public static <T> T doDelete(String url, Class<T> responseType, Map<String, Object> params) {
        try {
            HttpEntity<?> requestEntity = new HttpEntity<>(new HttpHeaders());
            ResponseEntity<T> responseEntity = restTemplate.exchange(
                    url, HttpMethod.DELETE, requestEntity, responseType, params
            );
            return responseEntity.getBody();
        } catch (RestClientException e) {
            throw new RuntimeException("DELETE 请求失败：" + url, e);
        }
    }
}
